//
//  XXMessageCell.m
//  QQ聊天
//
//  Created by 宋 on 16/3/11.
//  Copyright © 2016年 宋. All rights reserved.
//

#import "XXMessageCell.h"
#import "XXmessages.h"
#import "XXmessagefram.h"

@interface XXMessageCell()
@property(nonatomic,weak)UILabel* lblTime;
@property(nonatomic,weak)UIImageView *imgViewIcon;
@property(nonatomic,weak)UIButton * btnText;
@end

@implementation XXMessageCell


- (void)awakeFromNib {
    // Initialization code
}
//重写该initWithStyle初始化方法
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //创建子控件
        
        //1.显示时间的label控件
        UILabel *lblTime = [[UILabel alloc]init];
        //设置文字大小
        lblTime.font = [UIFont systemFontOfSize:12];
        //设置文字居中
        lblTime.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:lblTime];
        self.lblTime = lblTime;
        //2.显示头像的imageView控件
        UIImageView *imgViewIcon = [[UIImageView alloc]init];
        [self.contentView addSubview:imgViewIcon];
        self.imgViewIcon = imgViewIcon;
       
        //3.显示正文的按钮
        UIButton *btnText = [[UIButton alloc]init];
        //设置正文的文字大小
        btnText.titleLabel.font = textFont;
        //设置文字颜色
        [btnText setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        //正文文字换行
        btnText.titleLabel.numberOfLines = 0;
        //设置button的内边距
        btnText.contentEdgeInsets = UIEdgeInsetsMake(15, 20, 15, 20);
        [self.contentView addSubview:btnText];
        self.btnText = btnText;
        
        //self setFrame:<#(CGRect)#>
    }
    self.backgroundColor = [UIColor clearColor];
    return self;

}


//重写frame模型的set方法
-(void)setMessagefram:(XXmessagefram *)messagefram{
    _messagefram = messagefram;
    XXmessages *message = messagefram.message;
    //分别设置每个子控件的数据和frame
    //1.设置时间label的数据和frame
    self.lblTime.text = message.time;
    self.lblTime.frame = messagefram.timeFrame;
    
    //2.设置头像图片
    NSString *iconme = (message.type == XXmessagesTypeMe)?@"me":@"other";
    self.imgViewIcon.image = [UIImage imageNamed:iconme];
    self.imgViewIcon.frame = messagefram.iconFrame;
    
    //3.显示正文的按钮
    [self.btnText setTitle:message.text forState:UIControlStateNormal];
    self.btnText.frame = messagefram.textFrame;
    
    //设置文字颜色
    UIColor * fontcolor = (message.type == XXmessagesTypeMe)?[UIColor whiteColor]:[UIColor blueColor];
    [self.btnText setTitleColor:fontcolor forState:UIControlStateNormal];
    //设置背景图片

    NSString *iconName = (message.type == XXmessagesTypeMe)?@"chat_send_press_pic":@"chat_recive_press_pic";
    
    UIImage * iconImage = [UIImage imageNamed:iconName];
    //裁剪方法对背景图片进行处理
    iconImage = [iconImage stretchableImageWithLeftCapWidth:iconImage.size.width *0.5 topCapHeight:iconImage.size.height *0.5];
    //将背景图片贴合按钮的大小
    
    //将图片设置为背景图片给按钮
    [self.btnText setBackgroundImage:iconImage forState:UIControlStateNormal];
    self.btnText.frame = messagefram.textFrame;
    

}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
